//
//  CityInfoModel.m
//  CityInfoChooseView
//
//  Created by 正合适 on 2017/10/12.
//  Copyright © 2017年 正合适. All rights reserved.
//

#import "CityInfoModel.h"
#import <FMDB/FMDB.h>

#define kCityInfoTableName  @"jfjy_areas"
@implementation CityInfoModel
+ (NSArray <CityInfoModel *>*)loadCityInfoFromDatabaseId:(NSString *)theId {
    NSString *doc =[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES)  lastObject];
    NSString *fileName = [doc stringByAppendingPathComponent:@"shentong_scoring_deu.sqlite"];
    FMDatabase *db = [FMDatabase databaseWithPath:fileName];
    if ([db open]) {
        BOOL result = YES;
        if (result) {
            NSString *sqlStr = [NSString stringWithFormat:@"select * from %@ where parent_id=?",kCityInfoTableName];
            FMResultSet *set = [db executeQuery:sqlStr,theId];
            NSMutableArray *arr = [NSMutableArray array];
            while ([set next]) {
                CityInfoModel *model = [[CityInfoModel alloc] init];
                model.areaId = [set stringForColumn:@"id"];
                model.areaName = [set stringForColumn:@"name"];
                model.isSelected = NO;
                [arr addObject:model];
            }
            return arr;
        } else {
            return @[];
        }
    } else {
        return @[];
    }
}
@end
